package com.hardroid.workload.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;

import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;

import com.hardroid.workload.workcalc.ObsvDay;
import com.hardroid.workload.workcalc.Tally;

public class HardroidUtils {

	public static SimpleDateFormat formatXML = new SimpleDateFormat(
			"dd/MM/yyyy-HH:mm:ss Z", Locale.getDefault());
	public static SimpleDateFormat formatCurrTime = new SimpleDateFormat(
			"HH:mm:ss", Locale.getDefault());
	public static SimpleDateFormat formatTime = new SimpleDateFormat("HH:mm",
			Locale.getDefault());
	public static SimpleDateFormat formatDate = new SimpleDateFormat(
			"dd/MM/yyyy", Locale.getDefault());

	/**
	 * Get String representation of Time in Date
	 * 
	 * @param date
	 * @return String with format "HH:mm"
	 */
	public static String dateToTime(Date date) {
		return formatTime.format(date);
	}

	/**
	 * Get String representation of Date in Date
	 * 
	 * @param date
	 * @return String with format "dd/MM/yyyy"
	 */
	public static String dateToSDate(Date date) {
		return formatDate.format(date);
	}

	/**
	 * Get String representation of Date for file xml
	 * 
	 * @param date
	 * @return String
	 */
	public static String dateToXML(Date date) {
		return formatXML.format(date);
	}

	/**
	 * Parse the xml string representation of date to Date
	 * 
	 * @param xmlDate
	 * @return Date
	 */
	public static Date XMLDateToDate(String xmlDate) {
		Date date = null;
		try {
			date = formatXML.parse(xmlDate);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			date = new Date();
		}
		return date;
	}

	/**
	 * Get new Date after n minute of date
	 * 
	 * @param date
	 * @param n
	 * @return Date
	 */
	public static Date nextNMinuteDate(Date date, int n) {
		Date retval = new Date(date.getTime());
		retval.setMinutes(retval.getMinutes() + n);
		return retval;
	}

	/**
	 * Get next day of date
	 * 
	 * @param date
	 * @return Date
	 */
	public static Date nextDayDate(Date date) {
		Date retval = new Date(date.getTime());
		retval.setHours(retval.getHours() + 24);
		return retval;
	}

	public static Date beforeNMinuteDate(Date date, int n) {
		Date retval = new Date(date.getTime());
		retval.setMinutes(retval.getMinutes() - n);
		return retval;
	}

	/**
	 * Get string representation of current time
	 * 
	 * @return
	 */
	public static String getStringCurrentTime() {
		return formatCurrTime.format(new Date());
	}

	/**
	 * Copy List of Tally
	 * 
	 * @param l
	 * @return List
	 */
	public static ArrayList<Tally> copyTallyList(ArrayList<Tally> l) {
		ArrayList<Tally> retval = new ArrayList<Tally>();
		for (Tally t : l) {
			retval.add(new Tally(t));
		}
		return retval;
	}

	/** Mengecek apakah date adalah tanggal hari ini
	 * @param date
	 * @return
	 */
	public static boolean isDateToday(Date date) {
		Date today = new Date();
		return (today.getYear() == date.getYear())
				&& (today.getMonth() == date.getMonth())
				&& (today.getDate() == date.getDate());
	}

	/** True jika date1 dan date2 sama
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static boolean isDateEqual(Date date1, Date date2) {
		return (date1.getYear() == date2.getYear())
				&& (date1.getMonth() == date2.getMonth())
				&& (date1.getDate() == date2.getDate());
	}

	/** Mengcopy reference hari itu
	 * @param d
	 * @return
	 */
//	public static ObsvDay copyReference(ObsvDay d) {
//		// TODO Auto-generated method stub
//		ObsvDay day = new ObsvDay(d);
//		for (Tally t : day.lTally) {
//			t.description = "";
//			t.productive = false;
//			t.nProductive = false;
//			t.unknown = true;
//		}
//		return day;
//
//	}

//	public static void setListViewHeightBasedOnChildren(ListView listView) {
//		ListAdapter listAdapter = listView.getAdapter();
//		if (listAdapter == null) {
//			// pre-condition
//			return;
//		}
//
//		int totalHeight = 0;
//		for (int i = 0; i < listAdapter.getCount(); i++) {
//			View listItem = listAdapter.getView(i, null, listView);
//			listItem.measure(0, 0);
//			totalHeight += listItem.getMeasuredHeight();
//		}
//
//		ViewGroup.LayoutParams params = listView.getLayoutParams();
//		params.height = totalHeight
//				+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
//		listView.setLayoutParams(params);
//	}
}
